﻿using SosForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SosData;
using System.Data.Entity.Core.Objects;

namespace UOCmaker
{
    public partial class FormMdi : FormBase
    {

        #region " Constructor "

        /// <summary>
        /// Contructor
        /// </summary>
        public FormMdi()
        {
            InitializeComponent();
            FormPlantilla plantillaForm = new FormPlantilla();
            plantillaForm.MdiParent = this;
            plantillaForm.Show();
            timer.Start();

            this.tslblNombreUsuario.Text = SosData.Parameters.UserNickName;

        }

        #endregion

        #region " Menu "

        /// <summary>
        /// Ver avisos
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void avisosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAviso avisoFrm = new FormAviso();
            avisoFrm.MdiParent = this;
            avisoFrm.Show();
        }

        /// <summary>
        /// Ver tabla de tipologia
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tipologíaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTablaTipologia tipologiaFrm = new FormTablaTipologia();
            tipologiaFrm.MdiParent = this;
            tipologiaFrm.Show();
        }

        /// <summary>
        /// Ver tabla de grupos de plantillas
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void gruposToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTablaGrupo GrupoFrm = new FormTablaGrupo();
            GrupoFrm.MdiParent = this.MdiParent;
            GrupoFrm.Show();
        }

        /// <summary>
        /// Abrir gestor de plantillas
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void gestorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormPlantilla plantillaFrm = new FormPlantilla();
            plantillaFrm.MdiParent = this;
            plantillaFrm.Show();
        }



        DateTime FechaActual = DateTime.Today.Date;

        /// <summary>
        /// Timer tick
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void timer_Tick(object sender, EventArgs e)
        {
            using (var db = new sosContext())
            {
                var avisos = from a in db.avisos.Where(a => a.Mostrado == false && a.AvisarDia.Day == FechaActual.Day && a.AvisarDia.Month == FechaActual.Month && a.AvisarDia.Year == FechaActual.Year && a.AvisarHora == (DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString()).ToString() && a.usuarios.Contains(Parameters.UserNickName)) select a;

                foreach (Aviso _aviso in avisos)
                {
                    FormAviso avisoFrm = new FormAviso(_aviso);
                    avisoFrm.MdiParent = this;
                    _aviso.Mostrado = true;
                    if (_aviso.AvisarEmail)
                    {
                        //TODO enviar por correo el aviso
                    }
                    db.SaveChanges();
                    avisoFrm.Show();
                    notifyIcon.BalloonTipText = _aviso.Texto;
                    notifyIcon.BalloonTipTitle = _aviso.Asunto;
                    notifyIcon.ShowBalloonTip(20);
                }
            }
        }

        /// <summary>
        /// Exit
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

    }
}
